﻿namespace GenericHtml
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using WLFramework.Data;
    using WLFramework.DataAccess;

    public class BaseRepository<T> where T : class
    {
        public const string Configuration = "Default";

        public static T AddEntity(DbManager context, T entity)
        {
            SqlQuery<T> query = new SqlQuery<T>(context);
            query.Insert(entity);
            return entity;
        }

        public static bool UpdateEntity(DbManager context, T entity)
        {
            SqlQuery<T> query = new SqlQuery<T>(context);
            return query.Update(entity) > 0;
        }

        public static bool DeleteEntity(DbManager context, T entity)
        {
            SqlQuery<T> query = new SqlQuery<T>(context);
            return query.Delete(entity) > 0;
        }

        public static IQueryable<T> LoadEntities(DbManager context, Func<T, bool> where)
        {
            return context.GetTable<T>().Where(where).AsQueryable();
        }

        public static IQueryable<T> LoadPageEntities<S>(
            DbManager context,
            int page,
            int pageSize,
            out int total,
            Func<T, bool> where,
            bool asc,
            Func<T, S> orderby)
        {
            var table = context.GetTable<T>();
            total = table.Count();
            var query = asc ? table.OrderBy(orderby) : table.OrderByDescending(orderby);
            return query.Skip(pageSize * (page - 1)).Take(pageSize).AsQueryable();
        }

        private DbManager context;

        public DbManager Context
        {
            get { return context; }
        }

        protected BaseRepository()
        {
        }

        public BaseRepository(DbManager context)
        {
            this.context = context;
        }

        public T AddEntity(T entity)
        {
            SqlQuery<T> query = new SqlQuery<T>(context);
            query.Insert(entity);
            return entity;
        }

        public bool UpdateEntity(T entity)
        {
            SqlQuery<T> query = new SqlQuery<T>(context);
            return query.Update(entity) > 0;
        }

        public bool DeleteEntity(T entity)
        {
            SqlQuery<T> query = new SqlQuery<T>(context);
            return query.Delete(entity) > 0;
        }

        public IQueryable<T> LoadEntities(Func<T, bool> where)
        {
            return context.GetTable<T>().Where(where).AsQueryable();
        }

        public IQueryable<T> LoadEntities()
        {
            return context.GetTable<T>();
        }

        public IQueryable<T> LoadPageEntities<S>(
            int page,
            int pageSize,
            out int total,
            Func<T, bool> where,
            bool asc,
            Func<T, S> orderby)
        {
            var table = context.GetTable<T>();
            total = table.Count();
            var query = asc ? table.OrderBy(orderby) : table.OrderByDescending(orderby);
            return query.Skip(pageSize * (page - 1)).Take(pageSize).AsQueryable();
        }
    }
}